package com.yawn;

import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.util.zip.ZipInputStream;

/**
 * @author Created by yawn on 2017-12-09 14:32
 */
public class TestDeployBpmnModel {

    public static void main(String[] args) throws Exception {
        // 默认的流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 存储服务
        RepositoryService repositoryService = engine.getRepositoryService();
        // deploymentBuilder (部署builder)
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        // 添加bpmn模型并部署
        BpmnModel bpmnModel = createBpmnModel();
        deploymentBuilder.addBpmnModel("myProcess_Test", bpmnModel);
        ///
        // 关闭流程文件格式验证
//        deploymentBuilder.disableSchemaValidation();
        // 关闭流程文件流程验证
//        deploymentBuilder.disableBpmnValidation();
        deploymentBuilder.deploy();

        engine.close();
    }

    private static BpmnModel createBpmnModel() {
        // bpmn 模型
        BpmnModel bpmnModel = new BpmnModel();
        // 给模型添加流程
        Process process = new Process();
        process.setId("myProcess_Test");
        process.setName("my process Test Name");
        // 给流程添加事件
        StartEvent startEvent = new StartEvent();
        startEvent.setId("startEvent");
        UserTask userTask = new UserTask();
        userTask.setId("userTask");
        userTask.setName("User Task");
        EndEvent endEvent = new EndEvent();
        endEvent.setId("endEvent");
        process.addFlowElement(startEvent);
        process.addFlowElement(userTask);
        process.addFlowElement(endEvent);

        // 给流程添加顺序流
        process.addFlowElement(new SequenceFlow("startEvent", "userTask"));
        process.addFlowElement(new SequenceFlow("userTask", "endEvent"));
        // 给模型添加流程
        bpmnModel.addProcess(process);
        return bpmnModel;
    }
}
